package com.RecipeBook.main;

import java.util.List;

import com.RecipeBook.browse.Browse;
import com.RecipeBook.common.RecipeActivity;
import com.RecipeBook.cooking.CookingMain;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

/**
 * The main class for RecipeBook. It should instantiate the 
 * database, set up singletons, and so on and so forth, before
 * handing over control to Browse.
 */
public class RecipeMain extends RecipeActivity {
	
	GlobalsManager globals;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//setShortCut(this, "The Recipe Book");
		globals = GlobalsManager.getGlobals();
		
		// When done, redirect it to the Browse activity.
    	Intent intentB = new Intent(this, Browse.class);
		startActivity(intentB);
	}

	/**
	 * Sets up a shortcut to Recipe book in the desktop. Requires testing.
	 */
	public boolean setShortCut(Context context, String appName)
	{
		boolean flag = false ;
		int app_id = -1;
		PackageManager p = context.getPackageManager();
		Intent i = new Intent(Intent.ACTION_MAIN);
		i.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> res = p.queryIntentActivities( i,0);
		//System.out.println("the res size is: "+res.size());

		for(int k=0;k<res.size();k++)
		{
			if(res.get(k).activityInfo.loadLabel(p).toString().equals(appName)){
				flag = true;
				app_id = k;
				break;
			}
		}

		if(flag){
			ActivityInfo ai = res.get(app_id).activityInfo;

			Intent shortcutIntent = new Intent();
			shortcutIntent.setClassName(ai.packageName, ai.name);
			shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Intent intent = new Intent();
			intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

			intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);

			intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
			//intent.addCategory(Intent.CATEGORY_DEFAULT);
			intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
			context.sendBroadcast(intent);
		}
		else
			System.out.println("Applicaton not found");
		return true;
	}

}
